# coding: utf-8

from marshmallow import fields, validate

from app.api.utils import EmptyStrValidator
from app.products.constant import SceneType, ColorType, AssembleWay, Transport, \
    InstallWay, SPACING_LIST, WorkingCondition
from ..extensions import ma


class ConditionSchema(ma.Schema):
    """
    create by ld_wu 2017-09-18
    计算模型输入条件
    """
    working = fields.Integer(
        required=True,
        error_messages={
            'required': '工况为必填'
        },
        validate=validate.OneOf(
            WorkingCondition.mapping.keys(), error='工况不合法'
        )
    )

    scene_type = fields.Integer(
        required=True,
        error_messages={
            'required': '室内外为必填'
        },
        validate=validate.OneOf(
            SceneType.mapping.keys(), error='室内外不合法'
        )
    )

    install_way = fields.Integer(
        required=True,
        error_messages={
            'required': '挂装方式为必填'
        },
        validate=validate.OneOf(
            InstallWay.mapping.keys(), error='挂装方式不合法'
        )
    )

    color = fields.Integer(
        required=True,
        error_messages={
            'required': '色彩要求为必填'
        },
        validate=validate.OneOf(
            ColorType.mapping.keys(), error='色彩要求不合法'
        )
    )

    spacing = fields.String(
        required=True,
        error_messages={
            'required': '间距类型为必填'
        },
        validate=validate.OneOf(
            SPACING_LIST, error='间距类型不合法'
        )
    )

    assemble_way = fields.Integer(
        required=True,
        error_messages={
            'required': '组装方式为必填'
        },
        validate=validate.OneOf(
            AssembleWay.mapping.keys(), error='组装方式不合法'
        )
    )

    wide = fields.Decimal(
        required=True,
        error_messages={
            'required': '宽度为必填'
        }
    )

    high = fields.Decimal(
        required=True,
        error_messages={
            'required': '高度为必填'
        }
    )

    transport = fields.Integer(
        required=True,
        error_messages={
            'required': '信号方式方式为必填'
        },
        validate=validate.OneOf(
            Transport.mapping.keys(), error='信号方式不合法'
        )
    )

    products = fields.String(default=None)

    ground_high = fields.Decimal(default=0)


class OptionSchema(ConditionSchema):
    type_id = fields.String(
        required=True,
        error_messages={
            'required': '产品类型必填'
        }
    )

condition_schema = ConditionSchema()
option_schema = OptionSchema()


class ProductDetailSchema(ma.Schema):
    """
    create by ld_wu 2017-09-20
    """
    product_id = fields.Integer(
        required=True,
        error_messages={
            'required': '产品id为必填'
        },
        validate=EmptyStrValidator(error='产品id不能为空')
    )


product_detail_schema = ProductDetailSchema()


class ProductPriceSchema(ma.Schema):
    """
    create by ld_wu 2017-09-20
    """
    product_id = fields.Integer(
        required=True,
        error_messages={
            'required': '产品id为必填'
        },
        validate=EmptyStrValidator(error='产品id不能为空')
    )

    price = fields.Decimal(
        required=True,
        error_messages={
            'required': '价格为必填'
        },
        validate=EmptyStrValidator(error='价格不能为空')
    )


product_price_schema = ProductPriceSchema()
